const toggleForm = document.getElementById("toggleStockForm");
const buyStockForm = document.getElementById("buyStockForm");
const loadWarning = document.getElementById("tableJSWarning");


const func_toggleForm = function toggleBuyStocksVisibility(event) {
  if (loadWarning.hidden === true) 
     buyStockForm.hidden = !buyStockForm.hidden;
}

const func_buyStocks = async function (event) {
  event.preventDefault();
  showLoading();

  const buyStock = new FormData(buyStockForm),
        buyStockXHR = new XMLHttpRequest();
  buyStockXHR.addEventListener( 'load', function(event) {
    // DBG: alert("Aktien erfolgreich gekauft. Aktualisiere..");
    // update
    setNewStocks();
  } );

  buyStockXHR.addEventListener('error', function(event) {
    alert("Ein Fehler trat auf. Versuchen Sie es erneut.");
    // still update - this doesnt cause issues but possibly avoids
    // desync if the update came still through somehow
    setNewStocks();
  } );

  buyStockXHR.open("POST", "buyStocks");
  buyStockXHR.send(buyStock);
}

toggleForm.addEventListener("click", func_toggleForm, true);
buyStockForm.addEventListener("submit", func_buyStocks, true);
